package org.piwicode.bywordofmouth;

import com.google.common.collect.ImmutableList;
import com.google.common.io.Resources;
import com.googlecode.cqengine.CQEngine;
import com.googlecode.cqengine.IndexedCollection;
import org.piwicode.bywordofmouth.net.Client;
import org.piwicode.bywordofmouth.net.Server;
import org.piwicode.bywordofmouth.ui.SimpleSearch;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.LogManager;
import java.util.logging.Logger;

/**
 *
 * @author pierre
 */
public class Main {

    final Logger LOG = Logger.getLogger(Main.class.getName());

    public static void main(final String args[]) throws InterruptedException, IOException, ClassNotFoundException, InvocationTargetException, ExecutionException, URISyntaxException {
        try (InputStream is = Resources.getResource("lib/logging.properties").openStream()) {
            LogManager.getLogManager().readConfiguration(is);
        }
        //Start server
        final Share share = new Share("piwi");
        final ShareWorker shareWorker = new ShareWorker(share);
        //
        ImmutableList.Builder<Directory> builder = ImmutableList.builder();
        builder.add(new Directory("cs", new File("C:/CS").toURI(),share.rootResource()));
        /*builder.add(new Directory("software", new File("\\\\NEXUS\\software").toURI()));
         builder.add(new Directory("library", new File("\\\\NEXUS\\library").toURI()));
         builder.add(new Directory("music", new File("\\\\NEXUS\\music").toURI()));
         builder.add(new Directory("video", new File("\\\\NEXUS\\video").toURI()));
         */
        shareWorker.changeDirectories(builder.build()).get();

        final BackboneImpl backboneImpl = new BackboneImpl();
        backboneImpl.getShare().set(share);
        final Server srv = Server.start(backboneImpl, 8080);

        //Start client
        final Client client = Client.connect("localhost", 8080);
        final Backbone serviceStub = client.wrap(Backbone.class);
        final List<Resource> resources = serviceStub.getResources();
        System.out.printf("%d resources found on server\n", resources.size());
        final Resource resource = resources.get(0);
        final List<Hash> hashFor = serviceStub.getHashFor(resource);
        System.out.println(hashFor);

        //Perform a query
        System.out.println("Indexing");
        IndexedCollection<Resource> resourceIndexed = CQEngine.newInstance();
        //resourceIndexed.addIndex(SuffixTreeIndex.onAttribute(Resource.NAME));
        //resourceIndexed.addIndex(HashIndex.onAttribute(Resource.PARENT));
        resourceIndexed.addAll(resources);
        //SimpleTree.go(share.rootResource(),resourceIndexed);
        SimpleSearch.go(resourceIndexed);

        //Shutdown client
        System.out.println("Shutdown client");
        client.shutdown();
        //Shutdown server
        System.out.println("Shutdown server");
        //srv.shutdown();

    }
}
